package tv.youxing.android.ui;
/**
 * Created by apple on 2017/4/24.
 */

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;
import com.kymjs.rxvolley.http.VolleyError;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import tv.youxing.android.R;
import tv.youxing.android.custom.CustomCountDownTimer;
import tv.youxing.android.custom.CustomToast;
import tv.youxing.android.entity.PhoneCodeModel;
import tv.youxing.android.entity.RretrievePwdModel;
import tv.youxing.android.utils.GsonUtils;
import tv.youxing.android.utils.L;
import tv.youxing.android.utils.StaticClass;
import tv.youxing.android.utils.UtilTools;

/**
 * 项目名：   youxing
 * 包名：     tv.youxing.android .ui
 * 文件名：   SetPasswordActivity
 * 创建者：   叶克武
 * 创建时间： 2017/4/24 下午7:32
 * 描述：    TODO
 */
public class SetPasswordActivity extends BaseActivity {


    @BindView(R.id.phone)
    EditText etPhone;

    @BindView(R.id.sendCode)
    EditText etCode;

    @BindView(R.id.btn_sendCode)
    Button btnSendCode;

    @BindView(R.id.password)
    EditText password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_password);
        ButterKnife.bind(this);
    }


    @OnClick({R.id.btn_fini, R.id.btn_sendCode})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.btn_fini:
                UpdatePwd();
                break;
            case R.id.btn_sendCode:
                requestCode(etPhone.getText().toString().trim());
                break;
        }
    }


    void requestCode(String phone) {
        if (!UtilTools.isPhoneSuccess(phone)) {
            CustomToast.INSTANCE.showToast("手机号码不正确");
            return;
        }

        HttpParams params = new HttpParams();

        params.put("mobile", phone);

        RxVolley.post(StaticClass.sendCheckCodeURL, params, new HttpCallback() {
            @Override
            public void onSuccess(String t) {
                CustomToast.INSTANCE.showToast("onSuccess:"+t);


                PhoneCodeModel pd =GsonUtils.GsonToBean(t, PhoneCodeModel.class);


                CustomToast.INSTANCE.showToast(pd.getStatus());

                if (pd.getCode() == 200) {
                    CustomCountDownTimer customCountDownTimer = new CustomCountDownTimer(60000,1000,btnSendCode);
                    customCountDownTimer.start();
                }
            }

            @Override
            public void onFailure(VolleyError error) {
                super.onFailure(error);
                L.d("errorCode:" + error.getMessage());

                CustomToast.INSTANCE.showToast(error.getMessage());
            }
        });
    }


    /**
     * 重置密码
     */
    void UpdatePwd() {
        //post请求简洁版实现



        String pwd = UtilTools.getString(password);
        String phone = UtilTools.getString(etPhone);
        String code = UtilTools.getString(etCode);


        if (pwd.equals("") || phone.equals("") || code.equals("")) {
            CustomToast.INSTANCE.showToast("请填写完整");
            return;
        }


        HttpParams params = new HttpParams();
        params.put("password",pwd);
        params.put("mobile",phone);
        params.put("code",code);


        RxVolley.post(StaticClass.sretrievePwd, params, new HttpCallback() {
            @Override
            public void onSuccess(String t) {

                RretrievePwdModel model = GsonUtils.GsonToBean(t, RretrievePwdModel.class);

                if (model.getCode() == 200) {
                    CustomToast.INSTANCE.showToast("密码重置成功");

                    startActivity(new Intent(getApplicationContext(), LoginActivity.class));
                    finish();

                } else {
                    CustomToast.INSTANCE.showToast("error:" + model.getStatus()+"---"+model.getCode());
                }
            }
        });
    }
}
